package com.example.demo.web.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.SEmp;
import com.example.demo.common.utils.Response;
import com.example.demo.service.SEmpService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "员工信息服务接口")
@RestController
@RequestMapping("/emp")
public class SEmpController {
	@Autowired
	private SEmpService seService;
	@ApiOperation(value = "分页显示所有员工信息")
	@GetMapping("/all")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
	public Response findAllByPage(@RequestParam(name="pageNum" ,defaultValue = "1")int pageNum ,@RequestParam(name="pageSize" ,defaultValue = "3")int pageSize) {
		PageInfo<SEmp> page=seService.findAllByPage(pageNum, pageSize);
		return Response.success(page);
	}
	
	
	
	@GetMapping("/by/condition")
	@ApiOperation(value = "按照条件检索员工",notes = "按照id,用户名，部门id，start，end进行查找")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "编号"),
		@ApiImplicitParam(name = "username",value = "用户名"),
		@ApiImplicitParam(name = "dept_id",value = "部门编号"),
		@ApiImplicitParam(name = "start",value = "最早雇佣时间"),
		@ApiImplicitParam(name = "end",value = "最晚雇佣时间"),
		@ApiImplicitParam(name="pageNum",value = "页码"),
		@ApiImplicitParam(name = "pageSize",value = "每页个数")
	})
	public Response findByCondition(
			Integer id,
			String username,
			Integer dept_id,
			Date start,
			Date end,
			@RequestParam(defaultValue ="1")int pageNum,
			@RequestParam(defaultValue = "3")int pageSize) {
		
		PageInfo<SEmp> page=seService.findByCondition(id,username,dept_id,start,end,pageNum,pageSize);
		return Response.success(page);
	}
	@PostMapping("/saveorupdate")
	@ApiOperation(value = "保存或更新员工信息")
	public Response saveOrUpdate(SEmp se) {
		seService.saveOrUpdate(se);
		return Response.success(se.getId()==null?"添加员工信息成功":"保存员工信息成功");
	}
	@DeleteMapping("/del/by/id")
	@ApiOperation(value = "根据id删除")
	public Response deleteById(Integer id) {
		seService.deleteById(id);
		return Response.success("删除成功");
	}
	@DeleteMapping("/batch/del")
	@ApiOperation(value = "批量删除")
	public Response batchDeleteById(int[] id) {
		seService.batchDeleteById(id);
		return Response.success("批量删除成功");
	}
	
	@GetMapping("/count")
	@ApiOperation(value = "按照区域统计员工个数")
	public Response countEmp() {
		return Response.success(seService.countEmp());
	}
	@GetMapping("/username")
	@ApiOperation(value = "用户登入")
	public Response findUsername(String username) {
		return Response.success(seService.findUser(username));
	}
}
